package com.gr.smartedubackend.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.constant.CommonConstant;
import com.gr.smartedubackend.model.dto.student_score.StudentScoreQueryRequest;
import com.gr.smartedubackend.model.entity.StudentScore;
import com.gr.smartedubackend.service.StudentScoreService;
import com.gr.smartedubackend.mapper.StudentScoreMapper;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.stereotype.Service;

/**
 * @author 尘轩
 * @description 针对表【student_score(学生成绩)】的数据库操作Service实现
 * @createDate 2025-07-02 17:42:04
 */
@Service
public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreMapper, StudentScore>
        implements StudentScoreService {

    @Override
    public QueryWrapper<StudentScore> getQueryWrapper(StudentScoreQueryRequest studentScoreQueryRequest) {

        ThrowUtils.throwIf(studentScoreQueryRequest == null, ErrorCode.PARAMS_ERROR);

        Long studentId = studentScoreQueryRequest.getStudentId();
        String studentName = studentScoreQueryRequest.getStudentName();
        String courseName = studentScoreQueryRequest.getCourseName();
        Integer score = studentScoreQueryRequest.getScore();
        String gradeLevel = studentScoreQueryRequest.getGradeLevel();
        String sortField = studentScoreQueryRequest.getSortField();
        String sortOrder = studentScoreQueryRequest.getSortOrder();

        QueryWrapper<StudentScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(studentId), "studentId", studentId);
        queryWrapper.like(StrUtil.isNotEmpty(studentName), "studentName", studentName);
        queryWrapper.like(StrUtil.isNotEmpty(courseName), "courseName", courseName);
        queryWrapper.eq(ObjectUtil.isNotEmpty(score), "score", score);
        queryWrapper.eq(StrUtil.isNotEmpty(gradeLevel), "gradeLevel", gradeLevel);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            queryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            queryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return queryWrapper;
    }
}




